package com.hazy.animalface.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hazy.animalface.R;
import com.hazy.animalface.view.CreateStickerBagActivity;

import java.io.File;
import java.util.ArrayList;

import me.iwf.photopicker.PhotoPicker;
import me.iwf.photopicker.PhotoPreview;

/**
 * Created by Jiaui on 2017/4/15 0015.
 * <p/>
 * 上传头贴的列表适配器
 */
public class UploadStickerListAdapter extends RecyclerView.Adapter<UploadStickerListAdapter.MyViewHolder> {

    private final int MAX_STICKER_COUNT = 20;

    private Context context;
    private ArrayList<String> stickerList;

    public UploadStickerListAdapter(Context context, ArrayList<String> stickerList) {
        this.context = context;
        this.stickerList = stickerList;
    }

    @Override
    public UploadStickerListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_sticker_upload, parent, false));
    }

    @Override
    public void onBindViewHolder(UploadStickerListAdapter.MyViewHolder holder, int position) {

        if (stickerList == null || position == stickerList.size()) {
            holder.mStickerImage.setImageResource(R.mipmap.icon_add_sticker);
            holder.mStickerImage.setOnClickListener(addOnclickListener);
        } else {
            String stickerPath = stickerList.get(position);
            if (stickerPath != null) {
                holder.mStickerImage.setImageURI(Uri.fromFile(new File(stickerPath)));
                holder.mStickerImage.setOnClickListener(new EnlargeOnclickListener(position));
            }
        }
    }

    @Override
    public int getItemCount() {
        return stickerList == null ? 1 : stickerList.size() >= MAX_STICKER_COUNT ? MAX_STICKER_COUNT : stickerList.size() + 1;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        SimpleDraweeView mStickerImage;

        public MyViewHolder(View itemView) {
            super(itemView);
            mStickerImage = (SimpleDraweeView) itemView.findViewById(R.id.item_sticker_iv);
        }
    }

    private View.OnClickListener addOnclickListener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (stickerList != null && stickerList.size() >= MAX_STICKER_COUNT) {
                Toast.makeText(context, "选择的头贴数量已达上限", Toast.LENGTH_LONG).show();
                return;
            } else {
                PhotoPicker.builder()
                        .setPhotoCount(MAX_STICKER_COUNT - stickerList.size())
                        .setShowCamera(true)
                        .setPreviewEnabled(true)
                        .start((CreateStickerBagActivity) context, PhotoPicker.REQUEST_CODE);
            }
        }
    };

    private class EnlargeOnclickListener implements View.OnClickListener {

        private int position;

        public EnlargeOnclickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View view) {
            if (stickerList != null) {
                PhotoPreview.builder().setPhotos(stickerList).setCurrentItem(position)
                        .setShowDeleteButton(true).start((CreateStickerBagActivity) context,PhotoPreview.REQUEST_CODE);
            }
        }
    }
}
